package com.pandy.design.singleton;

/**
 * @description:
 * @author: panhong
 * @date: 2025/4/27
 */
public class Person {

    private String name;
    private Integer age;

    // 懒汉式
    private static volatile Person instance;

    // 饿汉式
    // private static Person instance = new Person();

    private Person() {
        System.out.println("创建Person对象");
    }

    /**
     * DCL : 双重检查锁
     * volatile + synchronized
     *
     * @return
     */
    public static Person getInstance() {
        if(instance == null) {
            synchronized(Person.class) {
                if(instance == null) {
                    instance = new Person();
                }
            }
        }
        return instance;
    }
}
